/**
 * @file    yuyv_camera_runner.cpp
 * @brief   yuyv工作线程
 * @author  Haozhong Xu
 * @date    2025-05-27
 * @version 1.0
 * 
 * @note    接受yuyv摄像头功能类，提取流到buffer中
 */
#ifndef YUYV_CAMERA_RUNNER_HPP
#define YUYV_CAMERA_RUNNER_HPP

#include "runner/base_runner.hpp"
#include "camera/yuyv_camera.hpp"
#include "buffer/thread_safe_queue.hpp"
#include "buffer/frame.hpp"

#include <memory>

/**
 * @class YUYVCameraRunner
 * @brief YUYVCameraRunner 工作线程类
 *
 * 接受YUYVCamera功能类对象和一个SharedBuffer共享
 * 队列，调用功能类对象的帧更新获取视频流到
 * 共享buffer中
 */
class YUYVCameraRunner : public RunnerBase {
public:
    YUYVCameraRunner(YUYVCamera &camera,
                     std::shared_ptr<ThreadSafeQueue<std::shared_ptr<Frame>>> output_queue)
        : RunnerBase("YUYVCameraRunner"), _camera(camera), _output_queue(std::move(output_queue)) {}

protected:
    void Run() override {
        while (IsRunning()) {
            if (_camera.UpdateFrame()) {
                const std::vector<uint8_t>& raw_data = _camera.GetFrame();

                auto frame = std::make_shared<Frame>();
                frame->width = _camera.GetWidth();
                frame->height = _camera.GetHeight();
                frame->format = FrameFormat::YUYV;
                frame->timestamp = std::chrono::steady_clock::now();
                frame->data = raw_data;

                _output_queue->Push(frame);  // 推入队列供下游处理
            }

            std::this_thread::sleep_for(std::chrono::milliseconds(30)); // 控制帧率
        }
    }

private:
    YUYVCamera &_camera;
    std::shared_ptr<ThreadSafeQueue<std::shared_ptr<Frame>>> _output_queue;
};

#endif // YUYV_CAMERA_RUNNER_HPP
